//
//  ESTSettingTableViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/11/1.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTSettingTableViewController.h"
#import "ESTLoginTool.h"
@interface ESTSettingTableViewController ()
/**
 *  标题
 */
@property(nonatomic, strong) NSArray<NSArray<NSString *> *> *titleArray;
@end

@implementation ESTSettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainTitle = [@"设置" S];
    self.clearsSelectionOnViewWillAppear = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ESTSettingTableViewControllerCell"];
}
- (void)didClickLogoutButton {
    WEAKSELF
    [TYZSystemDialog dialogWithTitle:[@"退出当前账号？" S] complement:^{
        [TYZLoadingView showMessage:nil];
        [ESTLoginTool logoutWithComplement:^(BOOL result) {
            [TYZLoadingView hideHUD];
            if (result) {
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }
        }];
    }];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == self.titleArray.count - 2) {
        return Value_Height_PartPadding * 3;
    }
    return Value_Height_PartPadding;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    TYZView *view = [[TYZView alloc] init];
    view.backgroundColor = ESTBackgroundColor;
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTSettingTableViewControllerCell" forIndexPath:indexPath];
    if (indexPath.section == self.titleArray.count - 1) {// 退出登录
        TYZButton *logoutButton = [TYZButton buttonWithType:UIButtonTypeCustom];
        [cell.contentView addSubview:logoutButton];
        [logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.contentView);
            make.bottom.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView).offset(30);
            make.right.equalTo(cell.contentView).offset(-30);
        }];
        cell.backgroundColor = TYZClearColor;
        [logoutButton setBackgroundColor:TYZWhiteColor];
        [logoutButton setTitle:self.titleArray.lastObject.firstObject forState:UIControlStateNormal];
        [logoutButton setTitleColor:TYZBlackColor forState:UIControlStateNormal];
        logoutButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
        logoutButton.layer.cornerRadius = CGRectGetHeight(cell.contentView.bounds) * 0.5;
        logoutButton.layer.borderWidth = 1.0;
        logoutButton.layer.borderColor = TYZLightMColor.CGColor;
        logoutButton.layer.masksToBounds = YES;
        [logoutButton addTarget:self action:@selector(didClickLogoutButton) forControlEvents:UIControlEventTouchUpInside];
    } else {
        cell.textLabel.text = self.titleArray[indexPath.section][indexPath.row];
        cell.textLabel.textColor = TYZBlackColor;
        cell.textLabel.font = TYZFont(CoreInfo.fontSize);
        if (indexPath.section == 0 || indexPath.section == 1) {
            TYZLabel *label = [[TYZLabel alloc] init];
            if (indexPath.section == 0) {
                label.text = [TYZCoreInfo currentCache];
            } else {
                label.text = CoreInfo.openNotification ? [@"开" S] : [@"关" S];
            }
            label.textColor = TYZBlackColor;
            label.font = TYZFont(CoreInfo.fontSize);
            [cell.contentView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.textLabel);
                make.right.equalTo(cell.contentView).offset(-20);
            }];
        } else {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == self.titleArray.count - 1) {
        return NO;
    }
    return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - setter & getter
- (NSArray<NSArray<NSString *> *> *)titleArray {
    if (_titleArray == nil) {
        _titleArray = @[@[[@"清除缓存" S]],
                        @[[@"消息通知" S]],
                        @[[@"版本升级" S], [@"修改密码" S], [@"关于App" S]],
                        @[[@"退出登录" S]]];
    }
    return _titleArray;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
